package com.gitee.sparta.core.cluster.strategy;

import com.gitee.sparta.common.helpers.IsolationHelper;
import com.gitee.sparta.core.bean.cluster.ClusterProtocol;
import com.gitee.sparta.core.cluster.balance.LoadBalance;
import com.gitee.sparta.core.property.SpartaCoreProperties;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;

import java.nio.channels.SocketChannel;

/**
 * <p>
 *      Leader 收到 follower 反馈的 pong 处理逻辑
 * </p>
 *
 * @author zyred
 * @since v 0.0.2
 */
@Slf4j
public class LeaderPongStrategy extends Strategy {

    public LeaderPongStrategy(RedissonClient client,
                              LoadBalance loadBalance,
                              SpartaCoreProperties properties) {
        super(client, loadBalance, properties);
    }

    @Override
    public void handle(ChannelHandlerContext ctx, SocketChannel sc, ClusterProtocol cp) {
        int serverId = cp.getServerId();
        String isolation = IsolationHelper.isolation(serverId);
        this.loadBalance.addNode(isolation, serverId);
        log.info("[Sparta] Pong command from follower: {}.", cp.getCmd());
    }

}
